package com.system.tree.web;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.system.common.util.ExtAjaxResponse;
import com.system.common.util.ExtPageable;
import com.system.permission.entity.Permission;
import com.system.tree.entity.Tree;
import com.system.tree.entity.DTO.TreeQueryDTO;
import com.system.tree.service.ITreeService;
@Controller
@RequestMapping("/tree")
public class TreeController 
{
	@Autowired
	private ITreeService treeService;

	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Tree tree) 
	{
		if(tree.getFatherId()!=null) {
		if(tree.getTreeId()==null) {
			try {
				Tree tree2 = treeService.findOne(tree.getFatherId());
				tree2.getChildren().add(tree);
				treeService.saveOrUpdate(tree);
				treeService.saveOrUpdate(tree2);
				return new ExtAjaxResponse(true,"菜单添加成功");
			}catch(Exception e){
				return new ExtAjaxResponse(false,"菜单添加失败");}
		}else{	
			try {		
				treeService.saveOrUpdate(tree);
				return new ExtAjaxResponse(true,"用户信息更新成功");
			}catch (Exception e) {
				return new ExtAjaxResponse(false,"用户信息更新失败");
			}	
				}
		}else {
			if(tree.getTreeId()==null) {
				try {
					treeService.saveOrUpdate(tree);
					return new ExtAjaxResponse(true,"菜单添加成功");
				}catch(Exception e){
					return new ExtAjaxResponse(false,"菜单添加失败");}
			}else{	
				try {		
					treeService.saveOrUpdate(tree);
					return new ExtAjaxResponse(true,"用户信息更新成功");
				}catch (Exception e) {
					return new ExtAjaxResponse(false,"用户信息更新失败");
				}	
					}
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse Delete(@RequestParam Long treeId) {
		try {
			Tree tree = treeService.findOne(treeId);
			treeService.delete(tree);
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败");
		}
	}
	@RequestMapping("/deleteTrees")
	public @ResponseBody ExtAjaxResponse Deletes(@RequestParam Long[] ids) {
		try {
			List<Long> lists = Arrays.asList(ids);
			if (null!=lists) {
				treeService.delete(lists);
			}
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败");
		}
	}
	@RequestMapping("/findOne")
	public @ResponseBody Tree findOne(@RequestParam Long treeId) 
	{
		Tree tree =  treeService.findOne(treeId);
		
		return tree;
	}
	@RequestMapping("/findAll")
	public @ResponseBody List<Tree> findAll() 
	{
		 List<Tree> lists =  treeService.findAll();
		
		return lists;
	}
	@RequestMapping("/findFather")
	public @ResponseBody List<Tree> findFather() 
	{
		 List<Tree> lists =  treeService.getFather(null);
		
		return lists;
	}
	@RequestMapping("/findPage")
	public @ResponseBody Page<Tree> findPage(TreeQueryDTO treeQueryDTO,ExtPageable pageable) 
	{
		Page<Tree> page =  treeService.findAll(TreeQueryDTO.getSpecification(treeQueryDTO), pageable.getPageable());
		
		return page;
	}
	
	@SuppressWarnings({ "unused", "unchecked" })
    @RequestMapping("/showmyMenu")
    @ResponseBody
    public List<Tree> myMenus(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        List<Permission> permissions = (List<Permission>) session.getAttribute("permissions");
        List<Tree> pris1 = new ArrayList<Tree>();//一级菜单
        for(Permission p : permissions) {
        	if(p.getTree()!=null&&p.getTree().getFatherId()==null) {
        	pris1.add(p.getTree());
        	}
        }
        List<Tree> menus = new ArrayList<Tree>();
        for(Tree pri:pris1){//迭代一级菜单找二级菜单
        	List<Tree> pris2 = new ArrayList<Tree>();//二级菜单
        	for(Permission p : permissions) {
            		if(p.getTree()!=null&&p.getTree().getFatherId()==pri.getTreeId()){
	            		pris2.add(p.getTree());
            	}
            }
            pri.setChildren(pris2);
            menus.add(pri);
        }
        System.out.println(menus);
        if (menus != null || menus.size() > 0) {
            return menus;
        } else {
            return null;
        }
    }
}
